<template>
  <div class="container">
    <div style="width:80%;margin: 0 auto;">
      <!-- 标题 -->
      <div class="title-container">
        <h3 class="title"><svg-icon icon-class="logo" /> Edums教学管理系统</h3>
      </div>
      <!-- 表单 -->
      <div class="login-container">
        <el-row :gutter="20">
          <el-col :xs="{span:0}" :md="{span:16}">
            <img src="@/assets/images/login_bg.jpg" height="500" width="100%">
          </el-col>
          <el-col :xs="{span:24}" :md="{span:8}">
            <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
              <h4>用户登录</h4>
              <el-form-item prop="username">
                <span class="svg-container">
                  <svg-icon icon-class="user" />
                </span>
                <el-input
                  ref="username"
                  v-model="loginForm.username"
                  placeholder="学号/工号"
                  name="username"
                  type="text"
                  tabindex="1"
                  auto-complete="on"
                />
              </el-form-item>

              <el-form-item prop="password">
                <span class="svg-container">
                  <svg-icon icon-class="password" />
                </span>
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model="loginForm.password"
                  :type="passwordType"
                  placeholder="密码"
                  name="password"
                  tabindex="2"
                  auto-complete="on"
                />
                <span class="show-pwd" @click="showPwd">
                  <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                </span>
              </el-form-item>

              <el-row>
                <el-col :span="19">
                  <el-form-item prop="captcha">
                    <span class="svg-container">
                      <svg-icon icon-class="captcha" />
                    </span>
                    <el-input
                      ref="captcha"
                      v-model="loginForm.captcha"
                      type="text"
                      placeholder="验证码"
                      name="captcha"
                      tabindex="3"
                      auto-complete="off"
                      @keyup.enter.native="handleLogin"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <span class="captcha-img" @click="switchCaptcha">
                    <img :src="captchaUrl" height="50" width="100">
                  </span>
                </el-col>
              </el-row>

              <div class="tips">
                <span style="margin-right:20px;">
                  <el-link>忘记密码？点击找回</el-link>
                </span>
              </div>

              <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>

              <h5>其它登录方式</h5>

              <el-row class="other-login">
                <el-col :span="8">
                  <svg-icon icon-class="dingtalk" class="login-icon" />
                  <h5>钉钉扫码登录</h5>
                </el-col>
              </el-row>

            </el-form>
          </el-col>
        </el-row>
      </div>
      <!-- 页脚 -->
      <el-footer>
        <el-divider />
        <h4>xx有限公司</h4>
      </el-footer>

    </div>

  </div>
</template>

<script>
import { validUsername, validCaptcha } from '@/utils/validate'
import { randInt } from '@/utils/random'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error('密码不少于3位'))
      } else {
        callback()
      }
    }
    const validateCaptcha = (rule, value, callback) => {
      if (!validCaptcha(value)) {
        callback(new Error('请输入验证码'))
      } else {
        callback()
      }
    }
    return {
      captchaUrl: '/api/auth/captcha/image',
      loginForm: {
        username: 'admin',
        password: '123456',
        captcha: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        captcha: [{ required: true, trigger: 'blur', validator: validateCaptcha }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    switchCaptcha() {
      // TODO 从后端获取验证码图片
      this.captchaUrl = `/api/auth/captcha/image?random=${randInt(1, 10000)}`
      console.log('刷新验证码')
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            // 刷新验证码
            this.switchCaptcha()
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#d6d8db;
$light_gray:#fff;
$cursor: #000;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #000;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#eee;
$dark_gray:#3b4348;
$light_gray:#fff;
$blue_sky:rgb(44, 121, 223);

.container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  margin: 0 auto;

  .login-form {
    // position: relative;
    width: 100%;
    color: black;
    // max-width: 100%;
    // // padding: 160px 35px 0;
    // margin: 0 auto;
    // overflow: hidden;
  }

  .captcha-img{
    cursor: pointer;
  }

  .tips {
    font-size: 14px;
    color: #000;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

   .other-login{
    text-align: center;
    cursor: pointer;
      .login-icon {
         height: 50px;
         width: 50px;
      }
   }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    margin-top: 2.6em;

    .title {
      font-size: 26px;
      color: $blue_sky;
      margin: 0px auto 40px auto;
      text-align: left;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
